import 'package:get/get.dart';

import '../../services/device_service.dart';
import 'manual_mode_state.dart';

class ManualModeController extends GetxController {
  final ManualModeState state = ManualModeState();

  final deviceService = Get.find<DeviceService>();

  int xMoveDistance = 0;
  int yMoveDistance = 0;
  int clearDosage = 0;
  String clearChannel = "";
  int addDosage = 0;
  String addChannel = "";
  String addPos = "";
  int defineVolumeDosage = 0;
  int shakeDuration = 0;
  int zMoveDistance = 0;
  int heatTemperature = 0;
  int heatDuration = 0;
  int coolingDuration = 0;
  String shakeSpeed = "";
  String defineVolumeChannel = "";
  String switchValveChannel = "";
  List<String> shakeSpeedItems = ["停止", "慢速", "中速", "快速"];
  List<String> channelItems = List.generate(9, (index) => "通道${index + 1}");
  List<String> posxItems = [];
  List<String> posyItems = [];
  List<String> samplePosItems = [];
  bool busConnected = false;
  int busState = -1;
  String selectedPosx = "";
  String selectedPosy = "";
  int absorbDosage = 0;
  String absorbPos = "";
  bool module1 = true;
  bool moduleSetting = false;

  @override
  void onInit() {
    shakeSpeed = shakeSpeedItems[0];
    defineVolumeChannel = channelItems[0];
    addChannel = channelItems[0];
    clearChannel = channelItems[0];
    switchValveChannel = channelItems[0];
    posxItems =
        List.generate(deviceService.sampleSize[0] + 1, (index) => "第${index}行");
    posyItems =
        List.generate(deviceService.sampleSize[1] + 1, (index) => "第${index}列");
    samplePosItems = List.generate(
        deviceService.sampleSize[0] * deviceService.sampleSize[1],
        (index) => "位置${index + 1}");
    selectedPosx = posxItems[0];
    selectedPosy = posyItems[0];
    addPos = samplePosItems[0];
    absorbPos = samplePosItems[0];
    deviceService.setBusConnectedListener((p0, p1) {
      busConnected = p0;
      busState = p1;
      update();
    });
    super.onInit();
  }

  int generateArg(int short1, int short2) {
    return (short1 << 24) & 0xffff0000 | short2 & 0xffff;
  }

  Future<bool> xGoHome() async {
    return await deviceService.componentAction(
        deviceService.armComponentId, deviceService.xMoveHome, 0);
  }

  Future<bool> xMove() async {
    return await deviceService.componentAction(deviceService.armComponentId,
        deviceService.xMoveAction, (xMoveDistance * 29.62962963).toInt());
  }

  Future<bool> yGoHome() async {
    return await deviceService.componentAction(
        deviceService.armComponentId, deviceService.yMoveHome, 0);
  }

  Future<bool> yMove() async {
    return await deviceService.componentAction(deviceService.armComponentId,
        deviceService.yMoveAction, (yMoveDistance * 29.62962963).toInt());
  }

  Future<bool> clear() async {
    return await deviceService.componentAction(
        deviceService.waterSupportComponentId,
        deviceService.clear,
        generateArg(int.tryParse(clearChannel.substring(2, 3))!, clearDosage));
  }

  Future<bool> add() async {
    print("==${addChannel}-$addPos");
    return deviceService.componentAction(
        deviceService.waterSupportComponentId,
        deviceService.add,
        int.tryParse(addPos.substring(2, addPos.length))! << 24 |
            int.tryParse(addChannel.substring(2, 3))! << 16 |
            addDosage);
  }

  Future<bool> switchValve() async {
    return deviceService.componentAction(
        deviceService.waterSupportComponentId,
        deviceService.switchValve,
        int.tryParse(switchValveChannel.substring(2, 3))!);
  }

  Future<bool> shake() async {
    return await deviceService.componentAction(
        deviceService.sampleBaseId,
        deviceService.shake,
        generateArg(shakeSpeedItems.indexOf(shakeSpeed), shakeDuration));
  }

  Future<bool> lift() async {
    return deviceService.componentAction(deviceService.sampleBaseId,
        deviceService.lift, (zMoveDistance * 29.62962963).toInt());
  }

  Future<bool> down() async {
    return deviceService.componentAction(
        deviceService.sampleBaseId, deviceService.down, 0);
  }

  Future<bool> cooling() async {
    return deviceService.componentAction(
        deviceService.sampleBaseId, deviceService.cooling, coolingDuration);
  }

  Future<bool> heatOn() async {
    return deviceService.componentAction(
        deviceService.heaterId, deviceService.heatOn, 0);
  }

  Future<bool> armMoveTest() async {
    return deviceService.componentAction(
        deviceService.armComponentId,
        deviceService.moveArm,
        // int.tryParse(selectedPosx.substring(1, 2))! << 16 |
        //     int.tryParse(selectedPosy.substring(1, 2))! & 0xFFFF);
        int.tryParse(selectedPosy.substring(1, 2))! << 16 |
            int.tryParse(selectedPosx.substring(1, 2))! & 0xFFFF);
  }

  Future<bool> absorbTest() async {
    return deviceService.componentAction(
        deviceService.absorberId,
        deviceService.absorbAction,
        int.tryParse(absorbPos.substring(2, absorbPos.length))! << 16 |
            absorbDosage);
  }

  Future<bool> abort() async {
    return deviceService.abortAction(deviceService.absorberId);
  }

  // Future<bool> initializeAction() async {
  //   return deviceService.initBoardAction();
  // }

  Future<bool> initializeAction() async {
    return await deviceService.componentAction(
        deviceService.armComponentId, deviceService.initAction, 0);
  }

  Future<bool> setModule() async {
    moduleSetting = true;
    final r = await deviceService.setAdjustment("module", module1 ? 0 : 1);
    moduleSetting = false;
    return r;
  }
}
